﻿#!/usr/bin/env python
# coding=utf-8

from google.appengine.ext import webapp
from web.web_controls import *

from models import Person
from gaesessions import get_current_session

from web.views import *
from group.models import *
from web_controls import *

class LoginHandler(ErrorMessageHandler):
  """用户登录页面"""
  def get(self, p):
    m = {
    "1": "请输入用户名",
    "2": "请输入密码",
    "3": "用户登录失败"
    }
    
    m2 = {
    "4": "请输入用户名",
    "5": "请输入密码",
    "6": "注册用户失败",
    "7": "两次输入的密码不匹配",
    "8": "密码不能小于6位"
    }
    
    
    login_error_message_list = []
    register_error_message_list = []
    
    if p != None:
      for i in p[1:]:
        if i in m:
          login_error_message_list.append(m[i])
        elif i in m2:
          register_error_message_list.append(m2[i])
    
    login_error_message = MessageList(login_error_message_list).to_html()
    register_error_message = MessageList(register_error_message_list).to_html()
    html = BasicHtml("person/templates/login.html", 
      { "login_error_message" : login_error_message, "register_error_message" : register_error_message }).to_html()
    self.response.out.write(html)

    
class LoginProcessHandler(webapp.RequestHandler):
  """处理用户登录请求"""
  def post(self):
    user_name = self.request.get("user_name")
    password = self.request.get("password")
    error_code = ""
    if len(user_name) == 0:
      error_code += "1"
    if len(password) == 0:
      error_code += "2"
      
    if len(error_code) == 0:
      if not Person.check_password(user_name, password):
        error_code = "3"
    
    if len(error_code) == 0:
      get_current_session()["person"] = user_name
      self.redirect("/person/home")
    else:
      self.redirect("/person/login/" + error_code)
  
class RegisterProcessHandler(webapp.RequestHandler):
  """注册用户处理"""
  def post(self):
    user_name = self.request.get("user_name")
    password = self.request.get("password")
    password2 = self.request.get("password2")
    
    error_code = ""
    if len(user_name) == 0:
      error_code += "4"
    if len(password) == 0:
      error_code += "5"
    
    if len(error_code) == 0:
      if password != password2:
        error_code += "7"
      elif len(password) < 6:
        error_code += "8"
      
    if len(error_code) == 0:
      passwd = password
      p = Person(key_name = user_name, password = passwd)
      if not p.save():
        error_code = "6"
        
    if len(error_code) == 0:
      get_current_session()["person"] = user_name
      self.redirect("/person/home")
    else:
      self.redirect("/person/login/" + error_code)
      
class ChangePasswordHandler(ErrorMessageHandler):
  """用户修改密码"""
  def get(self, p):
    if not "person" in get_current_session():
      self.redirect("/person/login")

    m = {
    "1": "输入不能为空",
    "2": "两次输入密码不对",
    "3": "修改密码错误",
    "4": "修改密码成功",
    "5": "密码不能小于6位"
    }
    error_message = MessageList(self.get_error_message_list(p, m)).to_html()
    html = BasicHtml("person/templates/change_password.html", { "error_message" : error_message }).to_html()
    self.response.out.write(html)
    
class ChangePasswordProcessHandler(webapp.RequestHandler):
  """修改密码处理过程"""
  def post(self):
    user_name = get_current_session()["person"]
    if user_name == None:
      self.redirect("/person/login")
    
    old_password = self.request.get("old_password")
    new_password = self.request.get("new_password")
    new_password2 = self.request.get("new_password2")
    
    error_code = ""
    
    if len(old_password) == 0 or len(new_password) == 0 or len(new_password2) == 0:
      error_code += "1"
    
    if len(error_code) == 0:
      if new_password != new_password2:
        error_code += "2"
      elif len(new_password) < 6:
        error_code += "5"
    
    if len(error_code) == 0:
      if not Person.change_password(user_name, old_password, new_password):
        error_code += "3"
    
    if len(error_code) == 0:
      self.redirect("/person/change_password/4")
    else:
      self.redirect("/person/change_password/" + error_code)
    
class LogoutProcessHandler(webapp.RequestHandler):
  """登出"""
  def get(self):
    del get_current_session()["person"]
    self.redirect("/person/login")
    
class HomeHandler(BaseHandler):
  """用户的主页"""
  def get(self):
    self.check_login()
    
    group_list = Group.get_groups_by_user(self.get_login_user())
    
    template_values = {
    "user_name" : self.get_login_user(),
    "group_list" : GroupList(group_list).to_html()
    }
    html = BasicHtml("person/templates/home.html", template_values).to_html()
    self.response.out.write(html)
    
    